package com.lepu.app.fun.index.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.DateUtil;
import com.app.utils.MyNetUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.index.bean.BeanWriter;

import java.util.ArrayList;

public class IndexWriterAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private Context mContext = null;
    private ArrayList<BeanWriter> mArrayList = null;

    public IndexWriterAdapter(Context context,
                              ArrayList<BeanWriter> arrayList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.index_writer_item, null);
            holder = new ViewHolder();
            holder.itemLayout = (LinearLayout) convertView.findViewById(R.id.item_layout);
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.content = (TextView) convertView.findViewById(R.id.content);
            holder.look = (TextView) convertView.findViewById(R.id.look);
            holder.comment = (TextView) convertView.findViewById(R.id.comment);
            holder.favor = (TextView) convertView.findViewById(R.id.favor);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BeanWriter item = mArrayList.get(position);
        UniversalImageLoadTool.disPlay(item.Cover, holder.image, R.drawable.index_banner_writer_icon_default);
        
        holder.title.setText(item.ArticleTitle);
        holder.time.setText(DateUtil.getHMTime(item.PublishDate));
        holder.content.setText(item.ArticleDescription);
        holder.look.setText(item.ViewCount+"");
        holder.comment.setText(item.CommentCount+"");
        holder.favor.setText(item.FavourCount+"");
        
        holder.itemLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				MyNetUtil.getInstance().requestArticleDetailWriter((BaseFragmentActivity)mContext, item.ArticleID);
			}
		});
        return convertView;
    }

    private static class ViewHolder {
    	LinearLayout itemLayout;
    	ImageView image;
        TextView title;
        TextView time;
        TextView content;
        TextView look;
        TextView comment;
        TextView favor;
        
    }

}
